package com.ymatou.iapi.trading.useranalysis.testcase;

import java.util.Arrays;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.trading.useranalysis.parameter.GetOrderCountSummaryReq;
import com.ymatou.iapi.trading.useranalysis.parameter.resp.BuyerOrderCount;
import com.ymatou.iapi.trading.useranalysis.parameter.resp.GetOrderCountSummaryResp;
import com.ymatou.iapi.trading.useranalysis.service.GetOrderCountSummaryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;

/**
 * 获取用户支付订单数量接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetOrderCountSummary {

	private static GetOrderCountSummaryReq req;
	private static GetOrderCountSummaryCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户支付订单数量");
	}

	@Before
	public void caseUp() {
		req = new GetOrderCountSummaryReq();
		call = new GetOrderCountSummaryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetOrderCountSummary_001() {
		Logger.start(true, "获取用户支付订单数量接口");
		try {
			
			OrderCallService orderCallService = new OrderCallService();
			
			int userId1 = orderCallService.createNewUser();
			int userId2 = orderCallService.createNewUser();

			TradingPlaceOrderCallService.placeOrderWithUserId(userId1);
			TradingPlaceOrderCallService.placeOrderWithUserId(userId2);

			req.setUserIds(Arrays.asList((long)userId1, (long)userId2));

			call.setData(req);
			call.callService();

			Logger.verifyEquals("200", call.getCode(), "验证code");

			Logger.verifyEquals("0", call.getBCode(), "验证bcode");

			Logger.verifyEquals(true, call.getData().getBoolean("success"),
					"验证success");

			GetOrderCountSummaryResp resp = call.getOrderCountSummaryResp();

			for (int i = 0; i < req.getUserIds().size(); i++) {

				BuyerOrderCount order = resp.getBuyerOrderCountList().get(i);

				Logger.verifyEquals(req.getUserIds().get(i), order.getUserId(),
						"验证id");

				Logger.verifyEquals(0L, order.getPayCount(), "验证PayCount");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetOrderCountSummary_002() {
		Logger.start(true, "获取用户支付订单数量接口");
		try {

			OrderCallService orderCallService = new OrderCallService();
			
			int userId1 = orderCallService.createNewUser();
			int userId2 = orderCallService.createNewUser();
			
			TradingCallService.payOrder(userId1);
			TradingCallService.payOrder(userId2);

			//等待数据同步
			Thread.sleep(20000);
			
			req.setUserIds(Arrays.asList((long)userId1, (long)userId2));

			call.setData(req);
			call.callService();

			Logger.verifyEquals("200", call.getCode(), "验证code");

			Logger.verifyEquals("0", call.getBCode(), "验证bcode");

			Logger.verifyEquals(true, call.getData().getBoolean("success"),
					"验证success");

			GetOrderCountSummaryResp resp = call.getOrderCountSummaryResp();

			for (int i = 0; i < req.getUserIds().size(); i++) {

				long id = req.getUserIds().get(i);

				BuyerOrderCount orderCount = resp.getBuyerOrderCountList()
						.stream().filter(order -> order.getUserId() == id)
						.collect(Collectors.toList()).get(0);

				Logger.verifyEquals(orderCount.getUserId(), id, "验证id");

				Logger.verifyEquals(1L, orderCount.getPayCount(), "验证PayCount");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetOrderCountSummary_003() {
		Logger.start(true, "userId 不存在");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService.placeOrderWithLuxury(false);

			long userId = placeOrderResp.getUserId();
			long userId2 = 111111L;

			req.setUserIds(Arrays.asList(userId, userId2));

			call.setData(req);
			call.callService();

			Logger.verifyEquals("200", call.getCode(), "验证code");

			Logger.verifyEquals("0", call.getBCode(), "验证bcode");

			Logger.verifyEquals(true, call.getData().getBoolean("success"),
					"验证success");

			GetOrderCountSummaryResp resp = call.getOrderCountSummaryResp();
			BuyerOrderCount order = resp.getBuyerOrderCountList().get(1);

			Logger.verifyEquals(req.getUserIds().get(1), order.getUserId(),
					"验证id");
			Logger.verifyEquals(0L, order.getPayCount(), "验证PayCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetOrderCountSummary_004() {
		Logger.start(true, "userId 为0");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService.placeOrderWithLuxury(false);

			long userId = placeOrderResp.getUserId();

			req.setUserIds(Arrays.asList(userId, 0L));

			call.setData(req);
			call.callService();

			Logger.verifyEquals("200", call.getCode(), "验证code");

			Logger.verifyEquals("0", call.getBCode(), "验证bcode");

			Logger.verifyEquals(true, call.getData().getBoolean("success"),
					"验证success");

			GetOrderCountSummaryResp resp = call.getOrderCountSummaryResp();
			BuyerOrderCount order = resp.getBuyerOrderCountList().get(1);

			Logger.verifyEquals(req.getUserIds().get(1), order.getUserId(),
					"验证id");
			Logger.verifyEquals(0L, order.getPayCount(), "验证PayCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetOrderCountSummary_006() {
		Logger.start(true, "UserIds 为null");
		try {

			call.setData(req);
			call.callService();

			Logger.verifyEquals("400", call.getCode(), "验证code");

			Logger.verifyEquals("100", call.getBCode(), "验证bcode");

			Logger.verifyEquals(false, call.getData().getBoolean("success"),
					"验证success");

			Logger.verifyEquals("userIds:userIds未提供", call.getMsg(),
					"验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}